<cffunction name="getChildrenQry" returntype="query" output="false" access="private">
	<cfargument name="tree_ID" required="true">
  <cfargument name="parentID" required="true">
	<cfquery name="getChilnQry" datasource="#request.dsource#">
		SELECT parent.tree_item_id as widgetid, parent.tree_item_display as title, parent.tree_item_order,
		max(kids.tree_item_id) as hasKids
		FROM dojotrees parent LEFT JOIN dojotrees kids ON parent.tree_item_id = kids.parent_tree_item_id
		WHERE parent.tree_ID = #val(tree_ID)# AND
		<cfif parentID eq "">
			(parent.parent_tree_item_ID IS NULL OR parent.parent_tree_item_ID = 0)
		<cfelse>
			parent.parent_tree_item_ID = <cfqueryparam cfsqltype="cf_sql_integer" value="#parentID#">
		</cfif>
		GROUP BY parent.tree_item_id, parent.tree_item_display,parent.tree_item_order
		ORDER BY parent.tree_item_order
	</cfquery>
	<cfreturn getChilnQry>
</cffunction>

<cffunction name="setNode" output="false">
	<cfargument name="nodeID" required="true">
	<cfargument name="objectID" default="">
	<cfargument name="title" default="">
	<cfargument name="parentID" default="">
	<cfargument name="index" default="">
	<cfargument name="treeID" default="">
	<cfset var returnMsg = "">
	<cftry>
	<cfif nodeID eq 0>
	  <cftransaction>
		<cfquery name="addChild" datasource="#request.dsource#">
			INSERT INTO dojotrees (tree_item_display,parent_tree_item_id,tree_id)
			VALUES (<cfqueryparam cfsqltype="cf_sql_varchar" value="#title#">,
				<cfqueryparam cfsqltype="cf_sql_integer" value="#parentID#">,
				<cfqueryparam cfsqltype="cf_sql_integer" value="#treeID#">)
		</cfquery>
		<cfquery name="getNewNode" datasource="#request.dsource#">
		  SELECT * FROM dojotrees 
		  WHERE tree_ID = <cfqueryparam cfsqltype="cf_sql_integer" value="#treeID#">
		  AND parent_tree_item_id = <cfqueryparam cfsqltype="cf_sql_integer" value="#parentID#">
		  AND tree_item_display = '#title#'
		</cfquery>
		</cftransaction>
		<cfreturn "({title:""#title#"",widgetId:""#getNewNode.tree_item_ID#""})">
	<cfelse>
		<cfquery name="updateNode" datasource="#request.dsource#" result="daRes">
			UPDATE dojotrees SET
			 <cfif treeID gt "">
			 	tree_ID = <cfqueryparam cfsqltype="cf_sql_integer" value="#treeID#">,			 
			 </cfif>
			 <cfif objectID gt "" AND objectID eq "tree">
			 	parent_tree_item_ID = <cfqueryparam cfsqltype="cf_sql_integer" value="" null="true">,
			 <cfelseif objectID gt "" AND objectID neq "tree">
			 	parent_tree_item_ID = <cfqueryparam cfsqltype="cf_sql_integer" value="#parentID#">,
			 </cfif>
			 <cfif index gt "">
			 	tree_item_order = <cfqueryparam cfsqltype="cf_sql_integer" value="#index#">,
			 </cfif>
			 <cfif title gt "">
			 	tree_item_display = <cfqueryparam cfsqltype="cf_sql_varchar" value="#title#">,
			 </cfif>
			 tree_item_ID = #nodeID#
			 WHERE tree_item_ID = <cfqueryparam cfsqltype="cf_sql_integer" value="#nodeID#">
		</cfquery>
		<cfsavecontent variable="daDumpq">
			<cfdump var="#daRes#">
		</cfsavecontent>
		<cfset session.funstuff = session.funstuff & daDumpq>
	</cfif>
	<cfcatch>
	  <cfset wee = addServerDebug(cfcatch)>
	  <cfset returnMsg = "error:""error setting node""">
	</cfcatch>
	</cftry>
	<cfif returnMsg eq "">
		<cfreturn "({})">
	<cfelse>
		<cfreturn "(#returnMsg#)">
	</cfif>
</cffunction>

<cffunction name="deleteNode" output="false">
	<cfargument name="nodeID">
	<cfquery name="check4kids" datasource="#request.dsource#">
	  SELECT * FROM dojotrees
	  WHERE parent_tree_item_ID = <cfqueryparam cfsqltype="cf_sql_integer" value="#nodeID#">
	</cfquery>
	<cfif check4kids.recordcount eq 0>
		<cfquery name="getNewNode" datasource="#request.dsource#">
		  DELETE FROM dojotrees
		  WHERE tree_item_ID = <cfqueryparam cfsqltype="cf_sql_integer" value="#nodeID#">
		</cfquery>
		<cfreturn true>
	<cfelse>
		<cfreturn false>
	</cfif>
</cffunction>
